package com.hp.bon.sgw.wh.channel.invoices.bizSlot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.invoices.BizSlot;

public class InvoiceInformationPatchSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(InvoiceInformationPatchSlot.class);

    @Override
    public BizResult doBiz(BizParam bizParam) {
        BizResult bizResult = new BizResult();

            //修改发票信息API  修改开票信息API

            _log.info("start INVOICEINFORMATION_PATCH......");
            String id = bizParam.getParam(BizConsts.KEY_INVOICEINFORMATION_PATCH_ID) == null ? "" : String.valueOf(bizParam.getParam(BizConsts.KEY_INVOICEINFORMATION_PATCH_ID));
            String json = bizParam.getParam(BizConsts.KEY_REQUEST_BODY) == null ? "" :
                    String.valueOf(bizParam.getParam(BizConsts.KEY_REQUEST_BODY));

            _log.info("bodyContentJSON......" + json);

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(json);

            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);

            webserviceUrl = webserviceUrl + "/" + id;
//            String resp = postWebserviceStr(buildStringMes, webserviceUrl,bizParam);
            String resp= HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl,bizParam);
            if (resp.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_SUCCESS);
                resp = HttpClientUtils.getErrorJsonStr(BizConsts.STATUS_ACOUNT_API_FAIL, "", "", "");
            }
            bizResult.setHttpBody(resp);
            _log.info("Responding to external messages====:{}",resp);

        return bizResult;
    }

//    private static String postWebserviceStr(StringBuilder buildStringMes, String webserviceUrl,BizParam bizParam) {
//        _log.info("===========webservice url===============" + webserviceUrl);
//        String resp = "";
//        _log.info("===========webservice url===============" + webserviceUrl);
//        PostMethod post = new PostMethod(webserviceUrl);
//        post.setRequestHeader("Content-Type",bizParam.getParamString(BizConsts.RQUEST_HEADER_CONTENT_TYPE));
//        post.setRequestHeader("X-APP-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY));
//        post.setRequestHeader("X-APP-KEY",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_APP_KEY));
//        post.setRequestHeader("X-CTG-Request-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_CTG_REQUEST_ID));
//        post.setRequestHeader("X-CTG-Region-ID",bizParam.getParamString(BizConsts.RQUEST_HEADER_X_CTG_REGION_ID));
//
//        post.setRequestHeader("SOAPAction", "");
//        try {
//            RequestEntity requestEntity = new StringRequestEntity(buildStringMes.toString(), "text/xml", "UTF-8");
//            post.setRequestEntity(requestEntity);
//            HttpClient client = new HttpClient();
//
//            client.getHttpConnectionManager().getParams().setConnectionTimeout(5 * 1000);
//            try {
//                client.executeMethod(post);
//            } catch (HttpException e) {
//                _log.info("===========webservice call timeout===============" + webserviceUrl);
//                return "";
//            } catch (IOException e) {
//                _log.info("===========webservice call timeout session_id:");
//                return "";
//            }
//            try {
//                _log.info("===========完成webservice===============" + resp);
//                InputStream str = post.getResponseBodyAsStream();
//                BufferedReader br = new BufferedReader(new InputStreamReader(str));
//                StringBuffer stringBuffer = new StringBuffer();
//                String result = "";
//                while ((result = br.readLine()) != null) {
//                    stringBuffer.append(result);
//                }
//                resp = stringBuffer.toString();
//                //resp = post.getResponseBodyAsString();
//            } catch (IOException e) {
//                _log.info("===========webservice call timeout===============" + webserviceUrl);
//                return "";
//            }
//        } catch (UnsupportedEncodingException e) {
//            _log.info("===========webservice call timeout===============" + webserviceUrl);
//            e.printStackTrace();
//        }
//        return resp;
//    }


}
